package cn.yitun.chat.yitunservermanagecore.general.manage.system.controller;

import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.ParamDefine;
import cn.yitun.chat.yitungeneral.annotation.parameter.RequestParameter;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.UserDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.system.UserRoleInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.system.user.UserInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunservercommonlib.enums.system.UserRoleTypeEnums;
import cn.yitun.chat.yitunservermanagecore.general.bridge.auth.annotation.PermissionMapping;
import cn.yitun.chat.yitunservermanagecore.general.manage.system.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 
 * date 2018-07-19 09:29:16<br>
 * description 管理员管理
 * @author yitun<br>
 * @since
 */
@Controller
@RequestMapping("/manage/system")
public class AdminController {
	@Resource
	UserService userService;

	@ResponseBody
	@RequestParameter
	@PermissionMapping(name = "管理员列表", perms = "sys:user:list")
	@RequestMapping(method = RequestMethod.POST, value = "/admin/list")
	public Object list(HttpServletRequest request,
					   @JsonDefine UserDto userQuery) {
		ResultMessage rm = new ResultMessage();
		try {
			PageHelper.startPage(userQuery.getCurrentPageNo(), userQuery.getPageSize());
			userQuery.setType(UserRoleTypeEnums.TYPE_ADMIN.getType());
			List<UserVo> list = userService.queryUserDataList(userQuery);
			rm.bodyPut("page", new PageInfo<>(list));
		} catch (Exception e) {
			e.printStackTrace();
			rm.addError("500", "系统异常");
		}
		return rm;
	}

	@ResponseBody
	@RequestParameter
	@PermissionMapping(name = "编辑管理员", perms = "sys:root:user:update")
	@RequestMapping(method = RequestMethod.POST, value = "/admin/addOrUpdate")
	public Object addOrUpdate(HttpServletRequest request,
							  @JsonDefine UserDto user) {
		ResultMessage rm = new ResultMessage();
		List<Long> roleIds = user.getRoleIds();
		try {
			user.setType(UserRoleTypeEnums.TYPE_ADMIN.getType());
			if (null == user.getId()) {
				UserInfo u = userService.getUserByAccount(user.getAccount());
				if (u == null) {
					user.setId(null);
					userService.addOrUpdate(user, roleIds);
				} else {
					rm.addWarning("001", "账号已存在！");
				}
			} else {
				UserInfo u = userService.getUserByAccount(user.getAccount());
				if (u == null) {
					userService.addOrUpdate(user, roleIds);
				} else {
					if (user.getId().equals(u.getId())) {
						userService.addOrUpdate(user, roleIds);
					}
				}
			}
			rm.bodyPut("user", user);
			rm.bodyPut("roleIds", roleIds);
		} catch (Exception e) {
			e.printStackTrace();
			rm.addError("500", "系统异常");
		}
		return rm;
	}

	@ResponseBody
	@RequestParameter
	@PermissionMapping(name = "获取管理员", perms = "sys:root:get_admin")
	@RequestMapping(method = RequestMethod.POST, value = "/admin/get")
	public Object get(HttpServletRequest request,
					  @JsonDefine UserDto query) {
		ResultMessage rm = new ResultMessage();
		try {
			UserInfo user = userService.getById(query.getUserId());
			rm.bodyPut("user", user);
		} catch (Exception e) {
			e.printStackTrace();
			rm.addError("500", "系统异常");
		}
		return rm;
	}

	@ResponseBody
	@RequestParameter
	@PermissionMapping(name = "删除管理员", perms = "sys:root:user:remove")
	@RequestMapping(method = RequestMethod.POST, value = "/admin/delete")
	public Object delete(HttpServletRequest request,
						 @ParamDefine("id") Long id) {
		ResultMessage rm = new ResultMessage();
		try {
			userService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			rm.addError("500", "系统异常");
		}
		return rm;
	}

	@ResponseBody
	@RequestParameter
	@PermissionMapping(name = "账号是否存在", perms = "sys:root:exist")
	@RequestMapping(method = RequestMethod.POST, value = "/admin/isExist")
	public Object isExistAccount(
			HttpServletRequest request,
			@ParamDefine("id") Long id,
			@ParamDefine("account") String account) {
		ResultMessage rm = new ResultMessage();
		boolean isExist = userService.isExistAccount(account, id);
		rm.bodyPut("exist", isExist);
		return rm;
	}

	@ResponseBody
	@RequestParameter
	@PermissionMapping(name = "获取管理员详情", perms = "sys:root:user:info")
	@RequestMapping(method = RequestMethod.POST, value = "/admin/getInfo")
	public Object loadInfo(
			HttpServletRequest request,
			@ParamDefine("id") Long id) {
		ResultMessage rm = new ResultMessage();
		try {
			UserInfo user = userService.getById(id);
			List<UserRoleInfo> userRoleInfoList = userService.getUserRoleInfoListByUserId(id);
			rm.bodyPut("user", user);
			rm.bodyPut("roleInfoList", userRoleInfoList);
		} catch (Exception e) {
			e.printStackTrace();
			rm.addError("500", "系统异常");
		}
		return rm;
	}

	@ResponseBody
	@RequestParameter
	@PermissionMapping(name = "修改密码", perms = "sys:root:user:updatePassword")
	@RequestMapping(method = RequestMethod.POST, value = "/admin/updatePassword")
	public Object saveUpdatePassword(ModelMap map,
			@ParamDefine("id") Long userId,
			@ParamDefine("password") String password) {
		ResultMessage rm = new ResultMessage();
		try {
			String text = "";
			int mark = 1;
			if (null == password || "".equals(password)) {
				text = ("密码不能为空！");
				mark = 0;
			}
			if (mark > 0) {
				mark = userService.updatePassword(userId, password);
				if (mark > 0) {
					text = ("修改成功！");
				} else {
					text = ("修改失败！");
				}
			}
			if (mark == 0) {
				rm.addWarning("001", text);
			}
		} catch (Exception e) {
			e.printStackTrace();
			rm.addError("500", "系统异常");
		}
		return rm;
	}
	
	
	@ResponseBody
	@RequestParameter
	@PermissionMapping(name = "取消管理员", perms = "sys:root:user:toGeneral")
	@RequestMapping(method = RequestMethod.POST, value = "/admin/toGeneral")
	public Object toGeneral(ModelMap map,
			@ParamDefine("id") Long userId,
			@ParamDefine("type") String type) {
		ResultMessage rm = new ResultMessage();
		try {
			String text = "";
			int mark = 1;
			if (mark > 0) {
				mark = userService.updateType(userId, UserRoleTypeEnums.TYPE_GENERAL.getType());
				if (mark > 0) {
					text = ("修改成功！");
				} else {
					text = ("修改失败！");
				}
			}
			if (mark == 0) {
				rm.addWarning("001", text);
			}
		} catch (Exception e) {
			e.printStackTrace();
			rm.addError("500", "系统异常");
		}
		return rm;
	}
}
